package org.social.config;

import java.io.FileNotFoundException;
import java.util.Date;
import java.util.List;

import javax.script.ScriptException;

import org.ConfigLoad;
import org.apache.activemq.jms.pool.PooledConnectionFactory;
import org.script.ScriptManager;
import org.social.comparable.PlayerLevelComparable;
import org.social.db.bean.PlayerMap;
import org.social.db.cache.PlayerMapCache;
import org.social.manager.InterceptManager;
import org.social.manager.NoticeManager;
import org.social.plugin.DBSavePlugin;
import org.social.plugin.VerifyCsvPlugin;
import org.social.quartz.MinuteJob;
import org.social.quartz.SecondsJob;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.utils.RemoteUtil;

import com.sojoys.artifact.config.Constants;
import com.sojoys.artifact.config.Modules;
import com.sojoys.artifact.config.Plugins;
import com.sojoys.artifact.core.IConfig;
import com.sojoys.artifact.core.IServer;
import com.sojoys.artifact.manager.ContextManager;
import com.sojoys.artifact.manager.PropManager;
import com.sojoys.artifact.manager.ThreadManager;
import com.sojoys.artifact.plugin.dirtyword.DirtyWordsPlugin;
import com.sojoys.artifact.plugin.id.SnowflakePlugin;
import com.sojoys.artifact.plugin.netty.web.NettyWebPlugin;
import com.sojoys.artifact.plugin.quartz.QuartzPlugin;
import com.sojoys.artifact.plugin.syncdatabase.SyncMysqlPlugin;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

/**
 * @author : DengYing
 * @CreateDate : 2017年7月31日 下午3:56:15
 * @Description ：Please describe this document
 */
public class SocialConfig implements IConfig {
	static Log log = LogFactory.get(SocialConfig.class);

	@Override
	public void configConstant(Constants me) {
	    try{
    		// 设置使用的配置文件
    		PropManager.use("app.properties");
    		// SpringContext初始化
    		ContextManager.me().init("app-init.xml");
            // 脚本初始化
            initScriptManager();
	    } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
	}

	@Override
	public void configModules(Modules me) {
	}

	@Override
	public void configPlugin(Plugins me) {
		// 注意：插件先后顺序影响 插件的启动和停止顺序(即为:先启动 先停止)

		// Mysql数据库同步插件
		me.add(new SyncMysqlPlugin());

		// 定时插件
		me.add(new QuartzPlugin());

		// 数据定时存储插件
		me.add(new DBSavePlugin());

		// Netty-WEB
		me.add(new NettyWebPlugin(null));

		// CSV同步插件
		me.add(new VerifyCsvPlugin());
		
		// 分布式ID插件
		me.add(new SnowflakePlugin());
	}

	@Override
	public void afterServerStart() {
		// 每秒定时器
		QuartzPlugin.addJob("SecondsJob", "SecondsJob", new Date(), "0/1 * * * * ?", SecondsJob.class, null);

		// 分钟定时器
		QuartzPlugin.addJob("MinuteJob", "MinuteJob", new Date(), "0 * * * * ? *", MinuteJob.class, null);

		// 向登陆服务器 通知社交服务器启动
		RemoteUtil.gameTopic().socialServerStart();

		// 向游戏服务器 通知社交服务器启动
		RemoteUtil.loginAsyn().socialServerStart();
		
		// 加载等级排行
		PlayerLevelComparable.loadAll();
		
//
//		// 加载拦截信息
//		// InterceptManager.loadInterceptInfo();
//		// 广播拦截消息
//		// InterceptManager.topicAllServerAddIntercept(InterceptManager.getAllInterceptInfo());
//
//		// 公告
//		NoticeManager.loadAllNotice();

		// 所有配置加载
		try {
			ConfigLoad.loadAllCsv(this.getClass().getClassLoader().getResource("gameConfig").getPath() + "/");
		} catch (Exception e) {
			e.printStackTrace();
		}

		log.info("************ Server Start Finish ************");
	}

	@Override
	public void beforeServerStop() {
		ThreadManager.me().stop();
		stopAcriveMQ();
		log.info("-> beforeServerStop");
	}
	
    /**
     * 初始化脚本系统
     * 
     * @throws ScriptException
     * @throws FileNotFoundException
     */
    private void initScriptManager() throws FileNotFoundException, ScriptException {
        ScriptManager.getInstance().initialize("scripts/java", "./scriptBin");
    }

	private void startActiveMQ() {
		// DefaultMessageListenerContainer topicListenerContainer =
		// ContextManager.me().getBean("topicListenerContainer",
		// DefaultMessageListenerContainer.class);
		// topicListenerContainer.start();

		DefaultMessageListenerContainer queueListenerContainer = ContextManager.me().getBean("queueListenerContainer",
				DefaultMessageListenerContainer.class);
		queueListenerContainer.start();
	}

	private void stopAcriveMQ() {
		// DefaultMessageListenerContainer topicListenerContainer =
		// ContextManager.getBean("topicListenerContainer",
		// DefaultMessageListenerContainer.class);
		// topicListenerContainer.destroy();

		DefaultMessageListenerContainer queueListenerContainer = ContextManager.me().getBean("queueListenerContainer",
				DefaultMessageListenerContainer.class);
		queueListenerContainer.destroy();

		PooledConnectionFactory pooledConnectionFactory = ContextManager.me().getBean("pooledConnectionFactory",
				PooledConnectionFactory.class);
		pooledConnectionFactory.stop();

		SingleConnectionFactory singleConnectionFactory = ContextManager.me().getBean("singleConnectionFactory",
				SingleConnectionFactory.class);
		singleConnectionFactory.destroy();
	}

	@Override
	public void startSucceed() {
		startActiveMQ();
	}

}
